<?php
class IndexAction extends AuthAction{
	
    public function index() {
		//G('begin');
		require_once APP_PATH.'/Common/common.php';

		$s_url = $_REQUEST['s_url'];
		
		//$s_url = 'http://www.discuz.net/forum.php?mod=viewthread&tid=3130477&page=1#pid25543109';

		preg_match('/[a-zA-z]+:\/\/([^\/]+)\//i', $s_url, $match);
		
		$host = $match[1];

		
		$do = str_replace('.','',$host);
		
		$checkArray = array(
			'ilaoyoucn' => 'wwwilaoyoucn'
		);
		
		$do = isset($checkArray[$do]) ? $checkArray[$do] : $do;
		
		$do2 = fxurl($s_url);
		
		//echo ucwords($do);		

        $do = ucwords($do);
		
		$do2 = ucwords($do2);
		
		//$doarray = array('ilaoyou','gxsky');
		
		// 判断 $mod 的合法性
		///$do = !in_array($do, $doarray) ? 'index' : $do;
		
		//import("@.ORG.Bbcode");
		//G('begin3');
		if(is_file(LIB_PATH.'/CollcetLib/'.'Collect'.$do.'.class.php')){
			$pageinfo = call_user_func('Collect'.$do.'::getPageContent', $s_url);
		}else if($do2 != "" && is_file(LIB_PATH.'/CollcetLib/'.'Collect'.$do2.'.class.php')){
			$pageinfo = call_user_func('Collect'.$do2.'::getPageContent', $s_url);
		}
	    //G('end3');
		//G('begin4');
		//echo trim($pageinfo['message']);exit;
		if(trim($pageinfo['message'])==""){
			require_once APP_PATH.'/Common/yzs1013_pldr.php';
			import("@.ORG.Zqnews");			
			$pageinfo = call_user_func('CollectTongyong::getPageContent', $s_url,$_GET['gather_type'], $pageinfo);						
		}
		//G('end4');

        
        $remote_ip = $this->GetRemoteIp();
        $site_url = $_REQUEST['site_url'];	
		/*	
		$Collect_log =  M('collect_log');
		$data['site_url'] =  $site_url;
		$data['remote_ip'] =  $remote_ip;
		$data['collect_url'] = $s_url;
		$data['collect_time'] = time();
		$Collect_log->add($data);	
		*/

		$html = "[".date('Y-m-d H:i:s', time())."]  ".$site_url."  ".$remote_ip."  ".$s_url."\r\n";
		file_put_contents(dirname(dirname(dirname(__FILE__)))."/collect_logs.txt",$html,FILE_APPEND);
			
		$data = array(
			'subject' => $pageinfo['title'],
			'message' => $pageinfo['message'],
			'from' => $pageinfo['from'],
			'status' => (strlen($pageinfo['message'])<100)?'error':'ok',
			's_url'=>$s_url
		);
		
	
		
		
		$json_data = json_encode($data);
		
		//G('end');
		/*
		echo '总共用时：'.G('begin','end').'s<br>';
		echo '获取网页：'.G('begin2','end2').'s<br>';	
		echo '第一步抓取：'.G('begin3','end3').'s<br>';
		echo '通用抓取：'.G('begin4','end4').'s<br>';
		//print_r($pageinfo);
		exit;	*/	
		
		if(trim($_REQUEST['request_type']) == 'post'){
			echo $json_data;
		}else{
			$callback = $_GET['jsoncallback'];
			echo $callback . '(' . $json_data . ')';
		}
		
    }
	
    public function GetRemoteIp($default='127.0.0.1')
    {
        $ip_string = $_SERVER['HTTP_CLIENT_IP'].','.$_SERVER['HTTP_X_FORWARDED_FOR'].','.$_SERVER['REMOTE_ADDR'];
        if ( preg_match ("/\d+\.\d+\.\d+\.\d+/", $ip_string, $matches) )
        {
            return $matches[0];
        }
        return $default;
    }	
}